package com.wangc.report.controller;

import com.wangc.report.service.GenerateService;
import com.wangc.report.utils.R;
import com.wangc.report.utils.RRException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("report/generate")
public class generateController {
    private static final Logger LOG = LoggerFactory.getLogger(generateController.class);

    @Autowired
    private GenerateService generateService;

    //http://127.0.0.1:8080/generate?table=test&dataDt=20200903
    @RequestMapping("/run")
    public R generateReport(@RequestParam Map<String, Object> params) {
        LOG.info("generateReport start ");
        LOG.debug(params.get("tableId").toString());
        String[] tableList = params.get("tableId").toString().split(",");
        LOG.debug(params.get("tableId").toString());
        if (tableList.length <= 0) {
            throw new RRException("输入表名为空请检查！");
        }
        if (null == params.get("dataDt")) {
            throw new RRException("输入数据日期为空请检查！");
        }
        String newOldFlg = params.get("newOldFlg").toString();
        LOG.debug(newOldFlg);
        for (String tableId : tableList) {
            generateService.generateReport(tableId, params.get("dataDt").toString());
        }
        LOG.info("generateReport over ");
        return R.ok().put("generateReport", "starting");
    }

}
